package node.block.stmt;

import midcode.MidCode;
import midcode.MidcodeGenerator;
import midcode.OPType;
import node.CompUnit;
import node.Node;
import node.block.Stmt;

public class ContinueStmt implements Node {
    private final Node father;
    private WhileStmt whileStmt;

    public ContinueStmt(Node father) {
        this.father = father;
    }

    private WhileStmt getWhileStmt() {
        Node node = this.father;
        while (node != null && !(node instanceof CompUnit)) {
            if (node instanceof Stmt && ((Stmt) node).stmt instanceof WhileStmt) {
                return (WhileStmt) ((Stmt) node).stmt;
            }
            node = node.getFather();
        }
        return null;
    }

    @Override
    public Node getFather() {
        return father;
    }

    @Override
    public String genMidCode() {
        whileStmt = getWhileStmt();
        assert whileStmt != null;
        String label = whileStmt.getLabel1();
        MidcodeGenerator mg = MidcodeGenerator.getInstance();
        mg.addMidcode(new MidCode(OPType.GOTO, null, null, label));
        return null;
    }
}

